package com.java.dormitorymanager.mvc.view;

import com.java.dormitorymanager.pojo.Dorm;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * @program: DormitoryManager
 * @description: 宿舍管理页面
 * @author: ShuangChenYue
 * @create: 2021-05-17 14:51
 **/
@WebServlet(value = "/ViewManageDorm")
public class ViewManageDormServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        System.out.println("您已进入宿舍管理页面");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html lang=\"en\">");
        out.println("<head>");
        out.println("<meta charset=\"UTF-8\">");
        out.println("<title>寝室管理界面</title>");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://localhost:8080/DormitoryManager/css/css-dorm/showDorm.css\">");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://localhost:8080/DormitoryManager/css/css-other/doIcon.css\">");
        out.println("</head>");
        out.println("<body>");
        out.println("<div id=\"divTitle\">");
        out.println("<h1 id=\"h01\">Manage Dorm</h1>");
        out.println("</div>");
        out.println("<div id=\"div02\">");
        out.println("<form action=\"/DormitoryManager/mvc/findDormByDornum.do\" method=\"post\">");
        out.println("<input id=\"inputSearch01\" type=\"text\" name=\"dornum\" placeholder=\"输入宿舍编号查找\"><input class=\"iconfontU\" id=\"inputSearch02\" type=\"submit\" name=\"search\" value=\"&#xe632;\" title=\"点击搜索\">");
        out.println("</form>");
        out.println("</div>");

        out.println("<div id=\"div01\">");
        out.println("<h3 class=\"ListName\">Man</h3>");
        out.println("<a  class=\"iconfontU addIcon\" title=\"添加宿舍\" href=\"/DormitoryManager/ViewAddDorm\">&#xe6d5;</a>");
        out.println("<a  class=\"iconfontU upIcon\" title=\"按照分数升序排列\" href=\"/DormitoryManager/mvc/showAllManDormUpWithScore.do\">&#xea7a;</a>");
        out.println("<a  class=\"iconfontU downIcon\" title=\"按照分数降序排列\" href=\"/DormitoryManager/mvc/showAllManDormDownWithScore.do\">&#xe658;</a>");
        out.println("<a  class=\"iconfontU defaultIcon\" title=\"默认排序\" href=\"/DormitoryManager/mvc/showAllManDormDefault.do\">&#xe601;</a>");
        out.println("<table class=\"table01\">");
        out.println("<tr class=\"tr01\">");
        out.println("<td>ID</td>");
        out.println("<td>宿舍编号</td>");
        out.println("<td>人数</td>");
        out.println("<td>类型</td>");
        out.println("<td>分数</td>");
        out.println("<td>操作</td>");
        out.println("</tr>");
        List<Dorm> allManDormList = (List<Dorm>) req.getAttribute("allManDormList");
        Iterator<Dorm> iterator = allManDormList.iterator();
        while (iterator.hasNext())
        {
            Dorm manDorm = iterator.next();
            out.println("<tr class=\"tr02\">");
            out.println("<td style=\"text-align: center\">"+manDorm.getId()+"</td>");
            out.println("<td>"+manDorm.getDornum()+"</td>");
            out.println("<td>"+manDorm.getAmount()+"</td>");
            out.println("<td>"+manDorm.getType()+"</td>");
            out.println("<td>"+manDorm.getScore()+"</td>");
            out.println("<td><a href=\"/DormitoryManager/mvc/toUpdateDormInfo.do?dornum="+manDorm.getDornum()+"&id="+manDorm.getId()+"\"><i class=\"iconfontU alterIcon\" title=\"修改信息\">&#xe700;</i></a> <a href=\"/DormitoryManager/mvc/deleteDormByDornum.do?dornum="+manDorm.getDornum()+"\"><i class=\"iconfontU deleteIcon\" title=\"删除\">&#xe614;</i></a> <a href=\"/DormitoryManager/mvc/showDormDetail.do?dornum="+manDorm.getDornum()+"\"><i class=\"iconfontU infoIcon\" title=\"详情\">&#xe611;</i></a> <a href=\"/DormitoryManager/mvc/addScoreToDorm.do?dornum="+manDorm.getDornum()+"&score="+manDorm.getScore()+"\"><i class=\"iconfontU scoreIcon\" title=\"打分\">&#xe606;</i></a> <a href=\"/DormitoryManager/mvc/toAddMSToDorm.do?dormNumber="+manDorm.getDornum()+"\"><i class=\"iconfontU addStudentIcon\" title=\"添加学生至本宿舍\">&#xe61f;</i></a></td>");
            out.println("</tr>");
        }
        out.println("</table>");


        out.println("<h3 class=\"ListName\">Woman</h3>");
        out.println("<a  class=\"iconfontU upIcon\" title=\"按照分数升序排列\" href=\"/DormitoryManager/mvc/showAllWomanDormUpWithScore.do\">&#xea7a;</a>");
        out.println("<a  class=\"iconfontU downIcon\" title=\"按照分数降序排列\" href=\"/DormitoryManager/mvc/showAllWomanDormDownWithScore.do\">&#xe658;</a>");
        out.println("<a  class=\"iconfontU defaultIcon\" title=\"默认排序\" href=\"/DormitoryManager/mvc/showAllWomanDormDefault.do\">&#xe601;</a>");
        out.println("<table class=\"table01\">");
        out.println("<tr class=\"tr01\">");
        out.println("<td>ID</td>");
        out.println("<td>宿舍编号</td>");
        out.println("<td>人数</td>");
        out.println("<td>类型</td>");
        out.println("<td>分数</td>");
        out.println("<td>操作</td>");
        out.println("</tr>");

        List<Dorm> allWomanDormList = (List<Dorm>) req.getAttribute("allWomanDormList");
        Iterator<Dorm> iterator1 = allWomanDormList.iterator();
        while (iterator1.hasNext())
        {
            Dorm womanDorm = iterator1.next();
            out.println("<tr class=\"tr02\">");
            out.println("<td style=\"text-align: center\">"+womanDorm.getId()+"</td>");
            out.println("<td>"+womanDorm.getDornum()+"</td>");
            out.println("<td>"+womanDorm.getAmount()+"</td>");
            out.println("<td>"+womanDorm.getType()+"</td>");
            out.println("<td>"+womanDorm.getScore()+"</td>");
            out.println("<td><a href=\"/DormitoryManager/mvc/toUpdateDormInfo.do?dornum="+womanDorm.getDornum()+"&id="+womanDorm.getId()+"\"><i class=\"iconfontU alterIcon\" title=\"修改信息\">&#xe700;</i></a> <a href=\"/DormitoryManager/mvc/deleteDormByDornum.do?dornum="+womanDorm.getDornum()+"\"><i class=\"iconfontU deleteIcon\" title=\"删除\">&#xe614;</i></a> <a href=\"/DormitoryManager/mvc/showDormDetail.do?dornum="+womanDorm.getDornum()+"\"><i class=\"iconfontU infoIcon\" title=\"详情\">&#xe611;</i></a> <a href=\"/DormitoryManager/mvc/addScoreToDorm.do?dornum="+womanDorm.getDornum()+"&score="+womanDorm.getScore()+"\"><i class=\"iconfontU scoreIcon\" title=\"打分\">&#xe606;</i></a> <a href=\"/DormitoryManager/mvc/toAddWSToDorm.do?dormNumber="+womanDorm.getDornum()+"\"><i class=\"iconfontU addStudentIcon\" title=\"添加学生至本宿舍\">&#xe61f;</i></a></td>");
            out.println("</tr>");
        }

        out.println("</table>");
        out.println("</div>");
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }
}
